from daos import get_db_session, get_update_contents
from models.album_record_material import AlbumRecordMaterial


def add_album_record_material(model_orm: AlbumRecordMaterial, is_commit=True):
    session = get_db_session()

    session.add(model_orm)
    session.commit() if is_commit else session.flush()
    return model_orm


def query_album_record_material(pk):
    return get_db_session().query(AlbumRecordMaterial).filter(AlbumRecordMaterial.id == pk).first()


def query_album_record_material_by_album_info_id(album_info_id):
    return get_db_session().query(AlbumRecordMaterial).filter(
        AlbumRecordMaterial.album_info_id == album_info_id).first()


def update_album_record_material(model_orm: AlbumRecordMaterial, *args, is_commit=True):
    session = get_db_session()
    update_contents = get_update_contents(model_orm, *args)
    session.query(AlbumRecordMaterial).filter(AlbumRecordMaterial.album_info_id == model_orm.album_info_id).update(update_contents)
    session.commit() if is_commit else session.flush()
    return
